package com.ktjy.config;

import com.ktjy.cond.ConditionOnPropertyEnable;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 配置类，主要是进行mybatis的相关的配置
 * @version v1.0
 * @Author JieGe
 * @Create 2024-12-12 - 9:38
 */
// <context:property-placeholder location="classpath:database.properties"/>
@PropertySource(value = "classpath:database.properties") // 读取database.properties配置文件
@Configuration  // 标识当前类为配置类
public class MyBatisConfig {

    @Value("${jdbc.driver}") // 用于读取database.properties配置文件中，key为jdbc.driver所对应值
    private String driver;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String user;

    @Value("${jdbc.password}")
    private String password;

    /*<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" scope="singleton">
		<property name="driverClassName" value="${jdbc.driver}"/>
		<property name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>*/

    @Bean
    @Conditional(ConditionOnPropertyEnable.OnEnableCondition.class)
    @ConditionOnPropertyEnable(enableProperty = "dev.enable")
    public BasicDataSource devDataSource(){
        BasicDataSource dataSource = new BasicDataSource(); // tab
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }

    /* <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 3.1 引入上一步配置的数据源对象 -->
		<property name="dataSource" ref="dataSource"/>
	</bean> */
    // 实战任务2
    @Bean
    @Conditional(ConditionOnPropertyEnable.OnEnableCondition.class)
    @ConditionOnPropertyEnable(enableProperty = "test.enable")
    // 读取database.properties中的test.enable,因为其值为false,所以当前方法就不会创建bean组件
    public BasicDataSource testDataSource(){
        BasicDataSource dataSource = new BasicDataSource(); // tab
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }
    // 创建SqlSessionFactoryBean组件，其实就是创建SqlSessionFactoryBean类的对象
    @Bean
    // <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    public SqlSessionFactoryBean sqlSession(){
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        // 引用testDataSoruce数据源
        sessionFactory.setDataSource(testDataSource()); // <property name="dataSource" ref="dataSource"/>
        return sessionFactory;
    }




}
